package com.company;

import com.utils.MapData;
import com.utils.FileUtils;
import com.utils.Map;
import com.utils.MyMapList;

import java.io.IOException;
import java.util.Scanner;

public class ArrayCRUD {


    /**
     * 读取文件
     * @return
     * @throws IOException
     */
    public MyMapList arrayReadFile() throws IOException {
        FileUtils fileUtils = new FileUtils();
        fileUtils.readFile("src\\com\\data\\ORDER_SAVE.txt");

        MyMapList maps = fileUtils.readFile("src\\com\\data\\ORDER_SAVE.txt");

        return maps;


    }

    /**
     * 将读取到的文件进行打印
     * @throws IOException
     */
    public void arrayReadFileToString() throws IOException {
       MapData.MyArray = arrayReadFile();
        for (int i =  0;i < MapData.MyArray.size();i++){
            System.out.println(MapData.MyArray.get(i).toString());
        }

        System.out.println("文件读取成功！！");
        System.out.println("文件数据大小" + MapData.MyArray.size());
    }


    /**
     * 顺序表插入
     * @throws IOException
     */
    public void arrayInsert() throws IOException {

        System.out.println("顺序表插入！！！");

//        ArrayReadFile ar = new ArrayReadFile();
//        MyMapList maps = ar.arrayReadFile();

        //MapData.MyArray = arrayReadFile();

        System.out.println("请分别输入道路信息中的：linkID、roadnameflag、branch、dispclass、roadname");

        //    #linkID=55410;roadnameflag=0;branch=0;dispclass=13;roadname=
        Map insertMap = new Map();
        System.out.print("请输入LinkId:");


        Scanner sc = new Scanner(System.in);

        int linkID = -1;
        while (true) {
            try {
                 linkID = sc.nextInt();
                break;
            } catch (Exception e) {
                System.out.println("错误有误,请重新输入");
            }
        }

        insertMap.setLinkID(linkID);
        System.out.print("请输入roadnameflag：");

        int roadnameflag = -1;
        while (true) {
            try {
                roadnameflag = sc.nextInt();
                break;
            } catch (Exception e) {
                System.out.println("错误有误,请重新输入");
            }
        }

        insertMap.setRoadnameflag(roadnameflag);
        System.out.print("请输入branch：");

        int branch = -1;
        while (true) {
            try {
                 branch = sc.nextInt();
                break;
            } catch (Exception e) {
                System.out.println("错误有误,请重新输入");
            }
        }
        insertMap.setBranch(branch);
        System.out.print("请输入dispclass：");


        int dispclass = -1;
        while (true) {
            try {
                dispclass = sc.nextInt();
                break;
            } catch (Exception e) {
                System.out.println("错误有误,请重新输入");
            }
        }

        insertMap.setDispclass(dispclass);
        System.out.print("请输入roadname：");

        String roadname = null;
        while (true) {
            try {
                roadname = sc.next();
                break;
            } catch (Exception e) {
                System.out.println("错误有误,请重新输入");
            }
        }
        insertMap.setRoadname(roadname);

        MapData.MyArray.add(insertMap);

        for (int i = 0; i < MapData.MyArray.size() ;i++)
            System.out.println(MapData.MyArray.get(i).toString());
        System.out.println("总共排序数据：" + MapData.MyArray.size() + "条");
        System.out.println("插入成功！！！");

    }

    /**
     * 顺序表查询！！！
     * @throws IOException
     */
    public void arraySerch() throws IOException {
        System.out.println("顺序表查询！！！");

//        ArrayReadFile ar = new ArrayReadFile();
//        MyMapList maps = ar.arrayReadFile();
//        MapData.MyArray = arrayReadFile();

        System.out.print("请输入需要查询的LinkID:");
        Scanner sc = new Scanner(System.in);
        int linkID = sc.nextInt();
        MapData.MyArray.searchById(linkID);
        System.out.println("查找成功：" + MapData.MyArray.searchById(linkID));

    }

    /**
     * 顺序表更改
     * @throws IOException
     */
    public void arrayUpate() throws IOException {
        System.out.println("根据linkID 修改顺序表修改道路信息");

//        ArrayReadFile ar = new ArrayReadFile();
//        MyMapList maps = ar.arrayReadFile();
//
//        MapData.MyArray = arrayReadFile();

        System.out.println("请分别输入道路信息中的：linkID");

        //    #linkID=55410;roadnameflag=0;branch=0;dispclass=13;roadname=
        Map updateMap = new Map();
        System.out.print("请输入LinkId:");
        Scanner sc = new Scanner(System.in);
        int linkID = sc.nextInt();
        updateMap.setLinkID(linkID);

        Map temp =  MapData.MyArray.searchById(linkID);
        System.out.println("查找到该LinkID对应的数据"+ temp.toString());

        System.out.println("请分别输入道路信息中的：roadnameflag、branch、dispclass、roadname");
        System.out.print("请输入roadnameflag：");
        int roadnameflag = sc.nextInt();
        updateMap.setRoadnameflag(roadnameflag);
        System.out.print("请输入branch：");
        int branch = sc.nextInt();
        updateMap.setBranch(branch);
        System.out.print("请输入dispclass：");
        int dispclass = sc.nextInt();
        updateMap.setDispclass(dispclass);
        System.out.print("请输入roadname：");
        String roadname = sc.next();
        updateMap.setRoadname(roadname);

        MapData.MyArray.updateById(linkID,updateMap);
        System.out.println("修改该道路信息成功：" + MapData.MyArray.updateById(linkID,updateMap).toString());

        for (int i = 0; i < MapData.MyArray.size() ;i++)
            System.out.println(MapData.MyArray.get(i).toString());
        System.out.println("总共数据：" + MapData.MyArray.size() + "条");

        //    maps.toString();
        System.out.println("修改成功！！！");
    }

    /**
     * 删除道路信息
     * @throws IOException
     */
    public void arrayDelete() throws IOException {

        System.out.println("根据linkID 删除顺序表道路信息");

//        MapData.MyArray = arrayReadFile();

        System.out.println("请分别输入道路信息中的：linkID");

        System.out.print("请输入LinkId:");
        Scanner sc = new Scanner(System.in);
        int linkID = sc.nextInt();

        int index = MapData.MyArray.getIndexById(linkID);

        MapData.MyArray.deleteEleByIndex(index);

        for (int i = 0; i < MapData.MyArray.size() ;i++)
            System.out.println(MapData.MyArray.get(i).toString());
        System.out.println("总共数据：" + MapData.MyArray.size() + "条");

        System.out.println("删除成功！！！");

    }


}



